######################
#  Replication code for 'Mediating the Electoral Connection', forthcoming in the JOP
#  John Henderson and John Brooks
#  12/7/2015    
######################    

# figure4-analysis.R
#  :: runs slider iv results by incumbent term in office 

rm(list=ls())
setwd('~/Dropbox/rainReplication')   

# loop over data and fixed effects specifications 
non.missings = 4
fes.type = 3
    
tars=1:13 
  
cnts=0                

coefs_clst=coefs=list()
coefs[[1]]=coefs[[2]]=matrix(NA,length(tars),3)
coefs_clst[[1]]=coefs_clst[[2]]=matrix(NA,length(tars),3)
           
for(i in 1:length(tars)){
	seniority=c(tars[i])

source('prelimRobust.R')  
                               
cnts=cnts+1  

#detach(covs)
# ANALYSIS             
   
main_iv1_fe=ivreg(vote~
	#as.factor(fe_id_num)+
	d_inc+
	dist_prev + midterm + pres_party + 
	black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted + 
	dose + dose_prv + vote_prv,
	~#as.factor(fe_id_num)+
	d_inc+
	dist_prev + midterm + pres_party + 
	black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted + 
	dose_prv + vote_prv + rain_day+rain_day_prev,
	subset=full,data=covs) 
main_iv2_fe=ivreg(vote~
	#as.factor(fe_id_num)+
	d_inc+
	dist_prev + midterm + pres_party + 
	black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted + 
	dose + dose_prv + vote_prv,
	~#as.factor(fe_id_num)+
	d_inc+
	dist_prev + midterm + pres_party + 
	black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted + 
	dose_prv + vote_prv + rain_weekend+rain_weekend_prev,
	subset=full,data=covs)

main_iv1_fe_sum=summary(main_iv1_fe)
main_iv2_fe_sum=summary(main_iv2_fe)
                               
main_iv1_fe_sumcl=coeftest.cluster(covs[full,],main_iv1_fe,cluster1='as.factor(fe_id_num)')
main_iv2_fe_sumcl=coeftest.cluster(covs[full,],main_iv2_fe,cluster1='as.factor(fe_id_num)')
        
   
coefs[[1]][cnts,c(1:3)]=main_iv1_fe_sum$coef[which(rownames(main_iv1_fe_sum$coef)=='dose'),c(1,2,4)]
coefs_clst[[1]][cnts,c(1:3)]=main_iv1_fe_sumcl[which(rownames(main_iv1_fe_sumcl)=='dose'),c(1,2,4)]

coefs[[2]][cnts,c(1:3)]		=main_iv2_fe_sum$coef[which(rownames(main_iv2_fe_sum$coef)=='dose'),c(1,2,4)]
coefs_clst[[2]][cnts,c(1:3)]=main_iv2_fe_sumcl	 [which(rownames(main_iv2_fe_sumcl)=='dose'),c(1,2,4)]

}

colnames(coefs[[1]])=colnames(coefs[[2]])=   
 colnames(coefs_clst[[1]])=colnames(coefs_clst[[2]])=
 c('Beta','SE','P')

rownames(coefs[[1]])=rownames(coefs[[2]])=   
 rownames(coefs_clst[[1]])=rownames(coefs_clst[[2]])=
 tars
     
coefs[[1]]=coefs[[1]][-c(1),]
coefs[[2]]=coefs[[2]][-c(1),]

save(coefs,coefs_clst,
file=paste('slider/figure4-',non.missings,'_',fes.type,'.Rdata',sep=''))     

rm(coefs,coefs_clst)
            
#detach(covs) 
#	}
#}    

# END      